
#include "zf_gpio.h"
#include "comparator.h"

#define COMPARATOR_MID_PIN  P3_6
#define COMPARATOR_A_PIN    P3_7
#define COMPARATOR_B_PIN    P5_1
#define COMPARATOR_C_PIN    P5_0


#define COMPARATOR_OUTPUT    0x0

//-------------------------------------------------------------------------------------------------------------------
//  @brief      比较器选择A通道为输入
//  @param      void                        
//  @return     void          
//  @since      v1.0
//  Sample usage:
//-------------------------------------------------------------------------------------------------------------------
void comparator_select_a(void)
{
    CMPEXCFG = 0x08;
}

//-------------------------------------------------------------------------------------------------------------------
//  @brief      比较器选择B通道为输入
//  @param      void                        
//  @return     void          
//  @since      v1.0
//  Sample usage:
//-------------------------------------------------------------------------------------------------------------------
void comparator_select_b(void)
{
    CMPEXCFG = 0x0a;
}

//-------------------------------------------------------------------------------------------------------------------
//  @brief      比较器选择C通道为输入
//  @param      void                        
//  @return     void          
//  @since      v1.0
//  Sample usage:
//-------------------------------------------------------------------------------------------------------------------
void comparator_select_c(void)
{
    CMPEXCFG = 0x09;
}

//-------------------------------------------------------------------------------------------------------------------
//  @brief      设置比较器上升沿触发中断
//  @param      void                        
//  @return     void          
//  @since      v1.0
//  Sample usage:
//-------------------------------------------------------------------------------------------------------------------
void comparator_rising(void)
{
    // 上升沿
    CMPCR1 = 0xAC | COMPARATOR_OUTPUT;
}

//-------------------------------------------------------------------------------------------------------------------
//  @brief      设置比较器下降沿触发中断
//  @param      void                        
//  @return     void          
//  @since      v1.0
//  Sample usage:
//-------------------------------------------------------------------------------------------------------------------
void comparator_falling(void)
{
    //下降沿
    CMPCR1 = 0x9C | COMPARATOR_OUTPUT;
}

//-------------------------------------------------------------------------------------------------------------------
//  @brief      获取比较结果
//  @param      void                        
//  @return     void          
//  @since      v1.0
//  Sample usage:
//-------------------------------------------------------------------------------------------------------------------
uint8 comparator_result_get(void)
{
    return (CMPCR1&0x01);
}

//-------------------------------------------------------------------------------------------------------------------
//  @brief      关闭比较器中断
//  @param      void                        
//  @return     void          
//  @since      v1.0
//  Sample usage:
//-------------------------------------------------------------------------------------------------------------------
void comparator_close_isr(void)
{
    CMPCR1 = 0x8C | COMPARATOR_OUTPUT;
}

//-------------------------------------------------------------------------------------------------------------------
//  @brief      比较器初始化
//  @param      void                        
//  @return     void          
//  @since      v1.0
//  Sample usage:
//-------------------------------------------------------------------------------------------------------------------
void comparator_init(void)
{
    gpio_mode(COMPARATOR_MID_PIN, GPI_IMPEDANCE); // 中性点
    gpio_mode(COMPARATOR_A_PIN  , GPI_IMPEDANCE); // A
	gpio_mode(COMPARATOR_B_PIN  , GPI_IMPEDANCE); // B
	gpio_mode(COMPARATOR_C_PIN  , GPI_IMPEDANCE); // C
    
    CMPCR1 = 0x8C;			// 1000 1100 打开比较器，P3.6作为比较器的反相输入端
	CMPCR2 = 63;			// 63个时钟滤波   比较结果变化延时周期数, 0~63
}

